package com.java01.file.inputstream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamToFile1 {

	public static void main(String[] args) {

	}
	
	public boolean inputStreamToFile(InputStream is, File destFile) {
		File destParent = destFile.getParentFile();
		if (!destParent.exists()) {
			destParent.mkdirs();
		}

		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(destFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		}

		byte[] buffer = new byte[1024];
		int length;
		try {
			while ((length = is.read(buffer)) > 0) {
				fos.write(buffer, 0, length);
				fos.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {}
			}
		}
		return true;
	}

}
